using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class GameControl : MonoBehaviour
{
    public bool ItemFlag
    {
        get
        {
            return itemFlag;
        }
        set
        {
            itemFlag = value;
            float time = UnityEngine.Random.Range(15, 20);
            StartCoroutine(SetTrueItemFlag(time));
        }
    }
    private bool itemFlag=true;
    public static GameControl Instance
    {
        get
        {
            if (instance == null)
            {
                instance = FindObjectOfType(typeof(GameControl)) as GameControl;
            }
            if(instance == null)
            {
                GameObject go = new GameObject("GameControlObject");
                instance = go.AddComponent<GameControl>();
                DontDestroyOnLoad(go);
            }
            return instance;
        }
    }
    
    private static GameControl instance;
    private GameControl()
    {
        
    }
    public delegate void Action();
    public event Action gameOverAction;
    public event Action gameStartAction;
    public void GameOver()
    {
        if(gameOverAction!=null)gameOverAction();
        
    }
    public void GameStart()
    {
        if (gameStartAction != null) gameStartAction();
    }
    public void clear()
    {
        if (gameOverAction != null)
        {
            Delegate[] dels = gameOverAction.GetInvocationList();
            foreach (Delegate de in dels)
            {
                gameOverAction -= de as Action;
            }
        }
        if(gameStartAction != null)
        {
            Delegate[] dels = gameStartAction.GetInvocationList();
            foreach (Delegate de in dels)
            {
                gameStartAction -= de as Action;
            }
        }

        
    }

    IEnumerator SetTrueItemFlag(float time)
    {
        yield return new WaitForSeconds(time);
        itemFlag = true;
    }
}
